package jude.java.money.article;

import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jude.java.util.Utils;
import jude.java.util.WebUtils;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;

public class GoArticlesGatherTask extends GatherTask {
	private static final String ENCODING = "UTF-8";

//	private static final String LINK_PATTERN = "<a href=\"(http://www\\.goarticles\\.com/cgi-bin/showa\\.cgi\\?C=(\\d+))\" target=\"newarticle\" class=\"article_title_link\">(.*?)</a>";
	private static final String LINK_PATTERN = "<a href=\"(/article/[^/]+/(\\d+)/)\" target=\"newarticle\" class=\"article_title_link\">(.*?)</a>";
	private static final String CONTENT_PATTERN = "</h1><br\\s/>(.*?)<br\\s/>\\s*<h1>About the Author</h1>";
	private static final String TAG_PATTERN = "</?\\w{1,10}>";
	public static final int PAGE_SIZE = 10;
	
//	public GoArticlesGatherTask(String... urls) {
//		super(urls);
//	}
	private String key;
	
	public GoArticlesGatherTask(String query, int start, int count) {
		super(getUrls(query, start, count));
		this.key = query;
	}

	protected void gather(String referer, String response) throws Exception {
		Pattern p = Pattern.compile(LINK_PATTERN, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
		Matcher m = p.matcher(response);
		int start = 0, count = 0;
		while (m.find(start)) {
			start = m.end(0);
			String url = m.group(1);
			String id = m.group(2);
			String title = m.group(3);
			System.out.print(++ count);
			System.out.print("\t" + id + "\t");
			System.out.print(url + "\t");
			System.out.println(title);
			if (!url.startsWith("http://")) {
				url = "http://goarticles.com" + url;
			}
			int r = gatherArticle(referer, title, id, url);
			if (r == 5) {
				denied = true;
				break;
			}
			Utils.sleep(1000);
		}
//		System.out.println(response);
	}

	private int gatherArticle(String referer, String title, String id, String url) {
		try {
			if (StringUtils.isEmpty(id)) {
				System.out.println("invalid id");
				return 1;
			}
			String storagePath = getStoragePath();
			File f = new File(storagePath);
			f.mkdirs();
			if (!f.exists()) {
				System.out.println("path invalid");
				return 2;
			}
			f = new File(storagePath + id + ".txt");
			if (f.exists() || new File(storagePath + "-" + id + ".txt").exists()) {
				System.out.println("exists");
				return 3;
			}
//			HttpClient client = new HttpClient();
//			PostMethod method = postMethod(url);
//			addParameters(method, id);
			GetMethod method = getMethod(url);
			int statusCode = client.executeMethod(method);
			if (statusCode != HttpStatus.SC_OK) {
				System.out.println(statusCode);
				return 4;
			}
			saveCookies(method);
			String response = WebUtils.getResponse(method, 81920, getEncoding());
			if (isDenied(response)) {
				System.out.println("denied");
				return 5;
			}
			Pattern p = Pattern.compile(CONTENT_PATTERN, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
			Matcher m = p.matcher(response);
			if (m.find()) {
				String content = m.group(1);
				// remove tags
				content = content.replaceAll(TAG_PATTERN, "");
				FileOutputStream out = new FileOutputStream(f);
				out.write(title.getBytes(getEncoding()));
				out.write(new byte[] {'\r', '\n'});
				out.write(content.getBytes(getEncoding()));
				out.close();
			} else {
				System.out.println("not found");
			}
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 6;
	}
	
	void addParameters(PostMethod post, String id) {
		post.addParameter("phrase", "iUhGtS");
		post.addParameter("C", id);
		post.addParameter("index", "1573");
	}
	
	protected boolean isDenied(String response) {
		boolean denied = response != null && response.indexOf("Enter the code from the image into the text box and hit enter to continue to the article") != -1;
		if (denied) {
			
		}
		return denied;
	}
	
	@Override
	protected String getSiteIdentify() {
		StringBuilder sb = new StringBuilder();
		sb.append("GoArticles").append(File.separator).append(key).append(File.separator);
//		sb.append(DateUtils.toString(new Date(), "MM-dd")).append(File.separator);
		return sb.toString();
	}
	
	@Override
	protected String getEncoding() {
		return ENCODING;
	}
	
	// http://www.goarticles.com/cgi-bin/search.cgi?term=ugg&l=10&c=none&D1=subject&s=11
	private static String[] getUrls(String query, int start, int count) {
		if (start % PAGE_SIZE != 0 || count % PAGE_SIZE != 0 || StringUtils.isEmpty(query))
			throw new IllegalArgumentException();
//		int pages = count / PAGE_SIZE;
//		String[] urls = new String[pages];
		String[] urls = new String[1];
		StringBuilder sb = new StringBuilder(32);
//		sb.append("http://www.goarticles.com/cgi-bin/search.cgi?term=");
//		sb.append("http://goarticles.com/search/?start=10&limit=10&q=");
		sb.append("http://goarticles.com/search/?q=");
		try {
			sb.append(URLEncoder.encode(query, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			sb.append(query);
		}
		sb.append("&start=").append(start);
		sb.append("&limit=").append(count);
//		sb.append("&l=").append(PAGE_SIZE);
//		sb.append("&c=none&D1=subject&s=");
//		int perfixLen = sb.length();
//		for (int i = 0; i < urls.length; i++) {
//			sb.setLength(perfixLen);
//			sb.append(start + i * PAGE_SIZE + 1);
//			urls[i] = sb.toString();
//		}
		urls[0] = sb.toString();
		return urls;
	}
}
